package ar.com.spsolutions.splilabrary.view.version2.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.ListModel;

import ar.com.spsolutions.splilabrary.view.pages.BasePage;

public class MenuPanel extends Panel {
	private static final long serialVersionUID = -8395930458588157917L;

	public MenuPanel(final Map<String, Class<? extends MainContainer>> menuMap) {
		super("menuPanel");
		this.createMenu(this.getMenuMap(menuMap));
	}

	private Map<String, Class<? extends MainContainer>> getMenuMap(
			final Map<String, Class<? extends MainContainer>> menuMap) {
		return (menuMap == null)//
		? new HashMap<String, Class<? extends MainContainer>>()//
				: menuMap;
	}

	@Override
	protected void onComponentTag(final ComponentTag tag) {
		super.onComponentTag(tag);
		this.setMarkupId("leftmenu_main");
	}

	private void createMenu(
			final Map<String, Class<? extends MainContainer>> menuMap) {
		new ArrayList<String>(menuMap.keySet());
		final List<Class<? extends MainContainer>> containers = this
				.getContainers(menuMap);
		final ListModel<Class<? extends MainContainer>> listModel = this
				.getListModels(containers);

		final ListView<Class<? extends MainContainer>> list = new ListView<Class<? extends MainContainer>>(
				"menu", listModel) {

			private static final long serialVersionUID = -4834797040262203515L;

			@Override
			protected void populateItem(
					final ListItem<Class<? extends MainContainer>> item) {

				final AjaxFallbackLink<Class<? extends MainContainer>> link = new AjaxFallbackLink<Class<? extends MainContainer>>(
						"menuRow") {

					private static final long serialVersionUID = 5145748835341847975L;

					@Override
					public void onClick(final AjaxRequestTarget target) {
						MenuPanel.this.onClick(item, target);
					}

				};
				link.add(new Label("text", new ResourceModel(MenuPanel.this
						.nameOf(item.getModelObject(), menuMap))));
				item.add(link);

			}
		};
		this.add(list);
	}

	private ListModel<Class<? extends MainContainer>> getListModels(
			final List<Class<? extends MainContainer>> containers) {
		return new ListModel<Class<? extends MainContainer>>(containers);
	}

	private List<Class<? extends MainContainer>> getContainers(
			final Map<String, Class<? extends MainContainer>> menuMap) {
		return new ArrayList<Class<? extends MainContainer>>(menuMap.values());
	}

	private void onClick(final ListItem<Class<? extends MainContainer>> item,
			final AjaxRequestTarget target) {
		try {
			final MainContainer container = item.getModelObject().newInstance();
			final BasePage page = (BasePage) this.getPage();
			page.setContainer(container);
			target.add(page);
		} catch (final Exception e) {
			throw new WicketRuntimeException(e);
		}
	}

	private String nameOf(final Class<? extends MainContainer> item,
			final Map<String, Class<? extends MainContainer>> menuMap) {
		final Iterator<Entry<String, Class<? extends MainContainer>>> iterator = menuMap
				.entrySet().iterator();
		while (iterator.hasNext()) {
			final Map.Entry<String, Class<? extends MainContainer>> entry = iterator
					.next();
			if (entry.getValue().equals(item)) {
				return entry.getKey();
			}
		}
		return null;
	}

}
